package com.app.shop.activitys;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.app.shop.MainActivity;

import com.app.library.base.BaseActivity;
import com.app.library.beans.UserBean;
import com.app.library.utils.SharedPreferencesUtil;
import com.app.library.utils.StringUtil;
import com.app.library.utils.ToastUtil;
import com.app.library.utils.UserManager;
import com.app.shop.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity implements View.OnClickListener {


    private  EditText inputName;

    private  EditText inputpwd;

    private TextView toLogin;

   private TextView tvRegister;



    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        inputName= (EditText) findViewById(R.id.inputName);
        inputpwd= (EditText) findViewById(R.id.inputpwd);
        toLogin= (TextView) findViewById(R.id.toLogin);
        tvRegister= (TextView) findViewById(R.id.tv_register);
        toLogin.setOnClickListener(this::onClick);
        tvRegister.setOnClickListener(this::onClick);
    }




    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    private void Login() {
        String name = inputName.getText().toString();
        String pwd = inputpwd.getText().toString();
        if (StringUtil.isEmpty(name)) {
            ToastUtil.showToast(this, "请输入用户名");
            return;
        }

        if (StringUtil.isEmpty(pwd)) {
            ToastUtil.showToast(this, "请输入密码");
            return;
        }


            boolean isHaveUser = UserManager.isHaveUser(name );
            if (isHaveUser) {//有该用户
                if (UserManager.isOk(name, pwd)) { //密码正确
                    UserBean userBean = UserManager.getUser(name);
                    SharedPreferencesUtil.saveDataBean(this, userBean, "user");
                    showActivity(this, MainActivity.class);
                    finish();
                } else {
                    ToastUtil.showToast(this, "密码不匹配");
                }
            } else {
                ToastUtil.showToast(this, "无此用户，请先注册");
            }


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toLogin:
                Login();
                break;
            case R.id.tv_register:
                Log.i("TAG", "onViewClicked: "+222222);
                showActivity(this, RegisterActivity.class);
                break;
        }
    }
}
